package com.k2data.etl;

import vcommon.utils.httpclient.HttpMethod;
import vcommon.utils.httpclient.HttpsUtil;
import vcommon.utils.httpclient.SimpleHttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jodd.io.FileUtil;
import org.apache.http.client.HttpClient;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class BQDtl {

    private String TO_IP = "192.169.2.22";

    String prefix = "cmm";
    String cartype = "c51";
    String subfix = "txt";// json
    String dataDir = "/home/vv/Projects/beiqi-data-collector/data/";

    boolean importMeta = false, importAssets = true, importData = true;

    public static void main(String[] args) {

        System.out.println(args[0]);

        JSONObject conf = null;
        try {
            String jsonConf = FileUtil.readString(args[0]);
            conf = JSON.parseObject(jsonConf);

            BQDtl bqDtl = new BQDtl();
            bqDtl.TO_IP = conf.getString("toIp");
            bqDtl.prefix = conf.getString("prefix");
            bqDtl.cartype = conf.getString("cartype");
            bqDtl.subfix = conf.getString("subfix");
            bqDtl.dataDir = conf.getString("dataDir");

            bqDtl.importMeta = conf.getBoolean("meta");
            bqDtl.importAssets = conf.getBoolean("asset");
            bqDtl.importData = conf.getBoolean("data");

            System.out.println(bqDtl.TO_IP);
            System.out.println(bqDtl.prefix);
            System.out.println(bqDtl.cartype);
            System.out.println(bqDtl.subfix);
            System.out.println(bqDtl.dataDir);
            System.out.println(bqDtl.importMeta);
            System.out.println(bqDtl.importAssets);
            System.out.println(bqDtl.importData);

            bqDtl.importDatas();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void importDatas() throws Exception {

        String path = dataDir + prefix + "_" + cartype;

        String path_meta = path + "/" + prefix + "_meta." + subfix;
        String path_assets = path + "/" + prefix + "_assets." + subfix;
        String path_data = path + "/" + prefix + "_data.json";

        if (importMeta) {
            File file1 = new File(path_meta);
            boolean success = importData(file1);
            if (!success) {
                return;
            }
        }

        if (importAssets) {
            File file2 = new File(path_assets);
            boolean success2 = importData(file2);
            if (!success2) {
                return;
            }
        }

        if (importData) {
            File file3 = new File(path_data);
            boolean success3 = importData(file3);
            if (!success3) {
                return;
            }
        }
    }

    private boolean importData(File file) throws Exception {
        String filename = file.getName();

        System.out.println(filename);

        if (filename.contains("asset")) {
            return import_assets(file);
        } else if (filename.contains("meta")) {
            return create_fgs(file);
        } else if (filename.contains("data")) {
            return send_data(file);
        }

        return true;
    }

    public boolean create_fgs(File file) throws Exception {

        String[] vs = FileUtil.readLines(file);
        String body = vs[0];

        String url = "http://" + TO_IP + ":8081/data-service/v2/field-groups";

        HttpClient client = HttpsUtil.getHttpClient();

        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "Basic azJkYXRhOksyRGF0YUBrMDAx");

        SimpleHttpResponse response = HttpsUtil.request(client, url, headers, HttpMethod.GET, body, null);
        System.out.println(response.getText());

        response = HttpsUtil.request(client, url, headers, HttpMethod.POST, body, null);

        System.out.println("META:" + response.getStatusCode() + ":" + response.getText());
        return response.getStatusCode() >= 200 && response.getStatusCode() < 300;
    }

    public boolean send_data(File file) throws Exception {
        String url = "http://" + TO_IP + ":8082/data-service/v2/channels/devices/data";

        String[] vs = FileUtil.readLines(file);

        HttpClient client = HttpsUtil.getHttpClient();

        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "Basic azJkYXRhOksyRGF0YUBrMDAx");
        SimpleHttpResponse response = null;

        for (String v : vs) {
            response = HttpsUtil.request(client, url, headers, HttpMethod.POST, v, null);
            System.out.println("DATA:" + response.getStatusCode() + ":" + response.getText());

            if (response.getStatusCode() >= 400) {
                return false;
            }
        }

        return true;

    }

    public boolean import_assets(File file) throws Exception {

        String url = "http://" + TO_IP + ":8081/data-service/v2/assets";

        String[] vs = FileUtil.readLines(file);

        HttpClient client = HttpsUtil.getHttpClient();

        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "Basic azJkYXRhOksyRGF0YUBrMDAx");
        SimpleHttpResponse response = null;

        for (String v : vs) {
            response = HttpsUtil.request(client, url, headers, HttpMethod.POST, v, null);
            System.out.println(response.getStatusCode() + ":" + response.getText());

            if (response.getStatusCode() >= 400) {
                System.out.println("Asset:" + response.getStatusCode() + " : " + response.getText());
//                return false;
            }
        }

        return true;

    }
}
